package com.zzl.alligator.management.system.service.impl;


import com.zzl.alligator.common.util.SpringUtils;
import com.zzl.alligator.management.system.domain.SysConfig;
import com.zzl.alligator.management.system.mapper.SysConfigMapper;
import com.zzl.alligator.management.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 参数配置表(SysConfig)服务层
 *
 * @author zhouziliang
 * @since 2020-06-30
 */
@Service
@CacheConfig(cacheNames = "CONFIG_CACHE")
@Slf4j
public class SysConfigService implements ISysConfigService {
    
    @Autowired
    private SysConfigMapper sysConfigMapper;
    
    /**
     * 通过ID查询单条数据
     *
     * @param configId 主键
     * @return 实例对象
     */
    @Override
    public SysConfig findSysConfigById(Integer configId) {
        return sysConfigMapper.findSysConfigById(configId);
    }

   /**
     * 通过实体作为筛选条件查询
     *
     * @param sysConfig 实例对象
     * @return 对象列表
     */
    @Override
    public List<SysConfig> findSysConfigList(SysConfig sysConfig) {
        return sysConfigMapper.findSysConfigList(sysConfig);
    }

    /**
     * 新增数据
     *
     * @param sysConfig 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CachePut(key = "#sysConfig.configKey")
    public int insertSysConfig(SysConfig sysConfig) {
        return sysConfigMapper.insertSysConfig(sysConfig);
    }

    /**
     * 修改数据
     *
     * @param sysConfig 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "#sysConfig.configKey")
    public int updateSysConfig(SysConfig sysConfig) {
        return sysConfigMapper.updateSysConfig(sysConfig);
    }

    /**
     * 通过主键删除数据
     *
     * @param configId 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "#configKey")
    public int deleteById(String configKey, Integer configId) {
        return sysConfigMapper.deleteById(configId);
    }

    /**
     * 根据配置键 查找配置信息
     *
     * @param configKey key
     * @return
     */
    @Override
    @Cacheable(key = "#configKey")
    public SysConfig findSysConfigByConfigKey(String configKey) {
        return sysConfigMapper.findSysConfigByKey(configKey);
    }

    /**
     * g根据配置的key，查找配置的值
     *
     * @param configKey 键
     * @return 值
     */
    @Override
    public String findConfigValueByConfigKey(String configKey) {
        //需要走代理，方能触发缓存动作。
        SysConfig sysConfig = SpringUtils.getAopProxy(this).findSysConfigByConfigKey(configKey);
        if(sysConfig == null) {
            return null;
        }
        return sysConfig.getConfigValue();
    }

    /**
     * 清空缓存
     */
    @Override
    @CacheEvict
    public void clearCache() {
        log.info("清除配置缓存");
    }
}